package com.yonyou.crm.sys.user.appctrl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.iuap.context.InvocationInfoProxy;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sys.user.entity.UserVO;
import com.yonyou.crm.sys.user.rmitf.IUserRmService;

@RequestMapping("/{version}/msys/users")
@RestController
public class AppUserCtrl {
	
	@Autowired
	private IUserRmService service;
	/**
	 * 获取当前用户负责人员
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/subusers", method = RequestMethod.GET)
	public Object getUsersByDeptId(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String deptidStr = paramObject.getString("deptid");
		String condition = paramObject.getString("condition");
		Long deptid = null;
		if(!"".equals(deptidStr)) {
			deptid = Long.parseLong(deptidStr);
		}
		List<Map<String, Object>> list = service.getUserByDeptId(deptid, condition);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("persons", list);
		return ResponseUtil.getObject(rsMap);
	}
	
	/**
	 * 获取当前公司用户列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object getUsersList(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		int page = paramObject.getInt("page");
		int pageSize = paramObject.getInt("pageSize");
		Map<String, Object> searchMap = new HashMap<String, Object>();
		// TODO...
		// searchMap = (Map<String, Object>)paramObject.get("condition");
			
		Page<UserVO> pagelist = service.getOrgUserList(new Page<UserVO>(page, pageSize), searchMap);
		List<UserVO> dataList = pagelist.getData();
		List<Map<String, Object>> list = new ArrayList<>();
		for(int i = 0; i < dataList.size(); i++) {
			UserVO user = dataList.get(i);
			Map<String, Object> userInfo = new HashMap<>();
			userInfo.put("id", user.getId().toString());
			userInfo.put("name", user.getName());
			userInfo.put("dept", user.getDeptName());
			list.add(userInfo);
		}
		
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("personlist", list);
		return ResponseUtil.getObject(rsMap);
	}
	
	/**
	 * 获取当前公司用户列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="dept", method = RequestMethod.POST)
	public Object getDeptAndPersonList(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		int deptId = paramObject.getInt("deptid");
		Map<String, Object> searchMap = new HashMap<String, Object>();
		searchMap.put("deptId", deptId);
		// TODO...
		// searchMap = (Map<String, Object>)paramObject.get("condition");

		Map<String, Object> res = service.getDeptAndUserList(searchMap);

		return ResponseUtil.getObject(res);
	}


	/**
	 * 获取当前登陆用户本部门及下级部门
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="subdepts", method = RequestMethod.GET)
	public Object getSubDepts(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		Map<String, Object> data = new HashMap<>();
		List<Map<String, Object>> depts = service.getSubdepts();
		data.put("depts", depts);
		return ResponseUtil.getObject(data);
	}

}
